/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.util;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;

/**
 * 扩展AnimatorValue以支持ofValue,setRepeatMode方法
 *
 * @since 2021-03-27
 */
public class OfAnimatorValue extends AnimatorValue
    implements AnimatorValue.ValueUpdateListener, Animator.LoopedListener, Animator.StateChangedListener {
    /**
     * 每次循环时从开始处
     */
    public static final int LOOP_MODE_RESTART = 1;
    /**
     * 循环时从上次结尾处开始
     */
    public static final int LOOP_MODE_REVERSE = 2;

    private float value = 1;
    private float start;
    private float end;
    private int loopMode = LOOP_MODE_RESTART;
    private boolean reverse = false;

    private ValueUpdateListener valueUpdateListener;
    private LoopedListener loopedListener;
    private StateChangedListener stateChangedListener;

    /**
     * 构造器
     */
    public OfAnimatorValue() {
        super.setValueUpdateListener(this);
        super.setStateChangedListener(this);
        super.setLoopedListenerInternal(this);
    }

    /**
     * 设置渐进动画范围区间
     *
     * @param startValue 开始
     * @param endValue 结束
     * @return 对象本身
     */
    public OfAnimatorValue ofValue(float startValue, float endValue) {
        this.value = endValue - startValue;
        this.start = startValue;
        this.end = endValue;
        return this;
    }

    /**
     * 设置动画时长
     *
     * @param duration 动画时长,单位好毫秒
     * @return 当前对象
     */
    public OfAnimatorValue duration(long duration) {
        super.setDuration(duration);
        return this;
    }

    /**
     * 设置循环模式
     *
     * @param mode LOOP_MODE_RESTART:从新开始; LOOP_MODE_REVERSE:反转
     * @return 当前对象
     */
    public OfAnimatorValue setRepeatMode(int mode) {
        loopMode = mode;
        return this;
    }

    @Override
    public void setValueUpdateListener(ValueUpdateListener listener) {
        this.valueUpdateListener = listener;
    }

    @Override
    protected void setLoopedListenerInternal(LoopedListener listener) {
        this.loopedListener = listener;
    }

    @Override
    public void setStateChangedListener(StateChangedListener listener) {
        this.stateChangedListener = listener;
    }

    @Override
    public void onUpdate(AnimatorValue animatorValue, float oriValue) {
        if (valueUpdateListener != null) {
            float convertValue = oriValue * value + start;
            if (reverse) {
                convertValue = end - convertValue + start;
            }
            valueUpdateListener.onUpdate(animatorValue, convertValue);
        }
    }

    @Override
    public void onRepeat(Animator animator) {
        if (loopMode == LOOP_MODE_REVERSE) {
            reverse = !reverse;
        }
        if (loopedListener != null) {
            loopedListener.onRepeat(animator);
        }
    }

    @Override
    public void onStart(Animator animator) {
        reverse = false;
        if (stateChangedListener != null) {
            stateChangedListener.onStart(animator);
        }
    }

    @Override
    public void onStop(Animator animator) {
        if (stateChangedListener != null) {
            stateChangedListener.onStop(animator);
        }
    }

    @Override
    public void onCancel(Animator animator) {
        if (stateChangedListener != null) {
            stateChangedListener.onCancel(animator);
        }
    }

    @Override
    public void onEnd(Animator animator) {
        if (stateChangedListener != null) {
            stateChangedListener.onEnd(animator);
        }
    }

    @Override
    public void onPause(Animator animator) {
        if (stateChangedListener != null) {
            stateChangedListener.onPause(animator);
        }
    }

    @Override
    public void onResume(Animator animator) {
        if (stateChangedListener != null) {
            stateChangedListener.onResume(animator);
        }
    }
}
